package cn.gorit.resource.controller;

import cn.gorit.common.lang.Result;
import cn.gorit.dto.resource.DownloadDto;
import cn.gorit.resource.service.FileService;
import com.aliyun.oss.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @version v1.0
 * @Classname FileController
 * @Description oss 对象存储控制层
 * @Author CodingGorit
 * @Created on 2021/4/14 15:02
 * @IDLE IntelliJ IDEA
 **/
@RestController
@RequestMapping("/api/v1/oss")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 文件上传，会覆盖相同的文件
     */
    @RequestMapping(value = "/upload",headers="content-type=multipart/form-data", method = RequestMethod.POST)
    public Result<Object> uploadFile (@RequestParam("file") MultipartFile file, @RequestParam String userId) {
        return fileService.upload(file,userId);
    }

    /**
     * 【平台】 获取 oss 中文件的存储列表
     */
    @GetMapping
    public Result<List<OSSObjectSummary>> getOssFileList(@RequestParam(value = "prefix",required = false) String prefix) {
        return fileService.getOssFileList(prefix);
    }

    /**
     * 文件删除
     * path 为数据库中保存的字段
     */
    @DeleteMapping(path = "/delete")
    public Result<Object> delete(@RequestParam("path") String path) {
        return fileService.delete(path);
    }

    /**
     * 文件下载
     */
    @PostMapping("/download")
    public void download(@RequestBody DownloadDto dto) {
        fileService.download(dto);
    }


}
